if (!THREE.Supports.webgl) {
  document.getElementById("oldie").style.display = "block";
}

var SCREEN_WIDTH = window.innerWidth,
    SCREEN_HEIGHT = window.innerHeight,
    r = 450,
    mouseX = 0, mouseY = 0,
    windowHalfX = window.innerWidth / 2,
    windowHalfY = window.innerHeight / 2,
    camera, scene, renderer,
    stats;

init();
setInterval(loop, 1000 / 60);

function init() {
  var container;
  container = document.createElement('div');
  document.body.appendChild(container);
  camera = new THREE.Camera(80, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 3000);
  camera.position.z = 1000;
  scene = new THREE.Scene();
  var i, line, vector1, vector2, material, p,
      parameters = [[0.25, 0xff7700, 1, 2], [0.5, 0xff9900, 1, 1], [0.75, 0xffaa00, 0.75, 1], [1, 0xffaa00, 0.5, 1], [1.25, 0x000833, 0.8, 1],
        [3.0, 0xaaaaaa, 0.75, 2], [3.5, 0xffffff, 0.5, 1], [4.5, 0xffffff, 0.25, 1], [5.5, 0xffffff, 0.125, 1]],
      geometry = new THREE.Geometry();

  for (i = 0; i < 1500; ++i) {
    vector1 = new THREE.Vector3(Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1);
    vector1.normalize();
    vector1.multiplyScalar(r);

    vector2 = vector1.clone();
    vector2.multiplyScalar(Math.random() * 0.09 + 1);

    geometry.vertices.push(new THREE.Vertex(vector1));
    geometry.vertices.push(new THREE.Vertex(vector2));
  }

  for (i = 0; i < parameters.length; ++i) {
    p = parameters[i];

    material = new THREE.LineBasicMaterial({color: p[1], opacity: p[2], linewidth: p[3]});

    line = new THREE.Line(geometry, material, THREE.LinePieces);
    line.scale.x = line.scale.y = line.scale.z = p[0];
    line.originalScale = p[0];
    line.rotation.y = Math.random() * Math.PI;
    line.updateMatrix();
    scene.addObject(line);
  }

  renderer = new THREE.WebGLRenderer();
  renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
  container.appendChild(renderer.domElement);

  /*
  stats = new Stats();
  stats.domElement.style.position = 'absolute';
  stats.domElement.style.top = '0px';
  container.appendChild(stats.domElement);
  */

  document.addEventListener('mousemove', onDocumentMouseMove, false);
  document.addEventListener('touchstart', onDocumentTouchStart, false);
  document.addEventListener('touchmove', onDocumentTouchMove, {passive: false});
}

function onDocumentMouseMove(event) {
  mouseX = event.clientX - windowHalfX;
  mouseY = event.clientY - windowHalfY;
}

function onDocumentTouchStart(event) {
  if (event.touches.length > 1) {

    event.preventDefault();

    mouseX = event.touches[0].pageX - windowHalfX;
    mouseY = event.touches[0].pageY - windowHalfY;
  }
}

function onDocumentTouchMove(event) {
  if (event.touches.length == 1) {

    event.preventDefault();

    mouseX = event.touches[0].pageX - windowHalfX;
    mouseY = event.touches[0].pageY - windowHalfY;
  }
}

//

function loop() {
  //camera.position.x += ( mouseX - camera.position.x ) * .05;
  camera.position.y += (-mouseY + 200 - camera.position.y) * .05;
  camera.updateMatrix();

  renderer.render(scene, camera);

  var time = new Date().getTime() * 0.0001;

  for (var i = 0; i < scene.objects.length; i++) {
    scene.objects[i].rotation.y = time * (i < 4 ? i + 1 : -(i + 1));
    if (i < 5) scene.objects[i].scale.x = scene.objects[i].scale.y = scene.objects[i].scale.z = scene.objects[i].originalScale * (i / 5 + 1) * (1 + 0.5 * Math.sin(7 * time));
  }

  //stats.update();
}